/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mytv.backbean;

import com.mytv.entity.Config;
import com.mytv.entity.MovieUser;
import com.mytv.service.ConfigService;
import com.mytv.service.MovieUserService;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

    
/**
 *
 * @author Sirius
 */
@ManagedBean
@ViewScoped
public class IndexBean extends BackBean {
    
    private String nickName;
    private String password;
    private boolean errorLogin;
    
    private List<String> categories;
    
    /**
     * Constructor of class
     */
    public IndexBean() {
        nickName = "";
        password = "";
        errorLogin = false;
    }

    /**
     * Get user service from spring context
     * @return A spring user service
     */
    public MovieUserService getUserService() {
        return (MovieUserService) getSpringBean(MovieUserService.class);
    }

    /**
     * Login the user
     */
    public void login() {
        errorLogin = false;
        MovieUser user = getUserService().getUser( getNickName(), encrypt(getPassword()));
        if(user != null) {
            getSessionBean().setUser(user);
            setPassword("");
            setNickName("");
        } else {
            errorLogin = true;
        }
        setPassword("");
    }

    /**
     * @return the nickName
     */
    public String getNickName() {
        return nickName;
    }

    /**
     * @param nickName the nickName to set
     */
    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
 
    /**
     * Get if get an error when login
     * @return <code>true</code>: If exist error when login<br/><code>false</code>: Otherwise
     */
    public boolean isErrorLogin() {
        return errorLogin;
    }
    
    public List<String> getCategories() {
        if(categories == null) {
            categories = new ArrayList<String>();
            String path = getPath();
            if(path != null) {
                File pathDirectory = new File(path);
                if(pathDirectory.exists() && pathDirectory.isDirectory()) {
                    String[] cats = pathDirectory.list();
                    categories.addAll(Arrays.asList(cats));
                    Collections.sort(categories);
                }
            }
        }
        return categories;
    }
    
    private ConfigService getConfigService() {
        return (ConfigService)getSpringBean(ConfigService.class);
    }
    
    public String getPath() {
        ConfigService service = getConfigService();
        if(service != null) {
            Config config = service.getConfig(ConfigService.PATH);
            if(config != null && config.getValue() != null && !config.getValue().isEmpty()) {
                String path = config.getValue().trim();
                if(!path.endsWith(File.separator)) {
                    path += File.separator;
                }
                return path;
            }
        }
        return null;
    }
    
    public List<String> getChilds() {
        List<String> childs = new ArrayList<String>();
        File file = getSessionBean().getFile();
        if(file != null && file.exists() && file.isDirectory()) {
            String[] cats = file.list();
            childs.addAll(Arrays.asList(cats));
            Collections.sort(childs);
        }
        return childs;
    }
    
    /**
     * Play selected chapter
     */
    public void play() {
        Config playerConfig = getConfigService().getConfig(ConfigService.PLAYER);
        if(playerConfig != null && playerConfig.getValue() != null) {
            try {
                File file = getSessionBean().getFile();
                if(file != null && file.exists() && file.isFile()) {
                    Runtime.getRuntime().exec(playerConfig.getValue() + " \"" + file.getAbsolutePath() + "\" -f");
                }
            } catch(Exception ex) {
            }
        }
    }
    
    /**
     * Pause selected chapter
     */
    public void pause() {
        try {
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_SPACE);
            robot.keyRelease(KeyEvent.VK_SPACE);
        } catch(Exception ex) {
        }
    }
    
    /**
     * Stop selected chapter
     */
    public void stop() {
        try {
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_Q);
            robot.keyRelease(KeyEvent.VK_Q);
            robot.keyRelease(KeyEvent.VK_CONTROL);
        } catch(Exception ex) {
        }
    }
    
    public void volPlus() {
        try {
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_CONTROL);
            for(int i = 0; i < 10; i++) {
                robot.keyPress(KeyEvent.VK_UP);
                robot.keyRelease(KeyEvent.VK_UP);
            }
            robot.keyRelease(KeyEvent.VK_CONTROL);
        } catch(Exception ex) {
        }
    }
    
    public void volMin() {
        try {
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_CONTROL);
            for(int i = 0; i < 10; i++) {
                robot.keyPress(KeyEvent.VK_DOWN);
                robot.keyRelease(KeyEvent.VK_DOWN);
            }
            robot.keyRelease(KeyEvent.VK_CONTROL);
        } catch(Exception ex) {
        }
    }
    
    public void screen() {
        try {
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_A);
            robot.keyRelease(KeyEvent.VK_A);
        } catch(Exception ex) {
        }
    }
    
    public void backward() {
        try {
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_LEFT);
            robot.keyRelease(KeyEvent.VK_LEFT);
            robot.keyRelease(KeyEvent.VK_CONTROL);
        } catch(Exception ex) {
        }
    }
    
    public void fordward() {
        try {
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_RIGHT);
            robot.keyRelease(KeyEvent.VK_RIGHT);
            robot.keyRelease(KeyEvent.VK_CONTROL);
        } catch(Exception ex) {
        }
    }
        
}